<template>
    <div>
        <H1>医生新增</H1>
        <table class="table table-bordered" style="background-color:cornflowerblue;">
            <tbody>
                <tr>
                    <td>医生姓名</td>
                    <td><input type="text" v-model="info.name"></td>
                </tr>
                  <tr>
                    <td>头像</td>
                    <td><input type="file" @change="img">
                        <img :src="info.img" width="60px" height="60px">
                    </td>
                </tr>
               
                <tr>
                    <td>科室</td>
                    <td> <select v-model="info.lid">
            <option value="">请选择</option>
            <option :value="item.oid" v-for="item in level" :key="item.oid">{{item.oname}}</option>  
        </select></td>
                </tr>
                <tr>
                    <td>第一执业医院</td>
                    <td><input type="text" v-model="info.onehospital"></td>
                </tr>
                <tr>
                    <td>创建日期</td>
                    <td><input type="date" v-model="info.createTimes"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import axios from 'axios';
import { onMounted } from 'vue';
onMounted(()=>{
    GetLevel();
})


let level:any=ref([]);
let info:any=ref({
  "id": 0,
  "name": "",
  "img": "",
  "onehospital": "",
  "createTimes": "",
  "lid": 0
})

const GetLevel=()=>{
      axios({
        url:'https://localhost:7118/api/Office/GetOfficeLevels',
        method:'get',
    })
    .then((res)=>[
        console.log(res),
        level.value=res.data
    ])
    .catch((err)=>{
        console.log(err);
    })
}
const img=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData();
    f.append('file',obj);
    axios({
        url:'https://localhost:7118/api/Img/Img',
        method:'post',
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="不是图片"){
            alert("您上传的不是图片");
            return;
        }
        if(res.data=="图片大于2M"){
            alert("上传的图片不能大于2M");
            return;
        }
        info.value.img=res.data
    })
}
const add=()=>{
    if(info.value.name==""){
        alert("医生姓名不能为空!")
        return;
    }
      if(info.value.onehospital==""){
        alert("第一执业医院不能为空")
        return;
    }
      if(info.value.createTimes==""){
        alert("创建日期不能为空")
        return;
    }
    axios({
        url:'https://localhost:7118/api/Hospital',
        method:'post',
        data:info.value
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1){
            alert("医生名称重复了!");
            return;
        }
        alert("添加成功");
        location.href='/doctorshow'
    })
    .catch((err)=>{
        console.log(err);
    })
}

</script>

<style scoped>

</style>